package xin.yogee.ms.module.sys.controller;


import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import xin.yogee.ms.common.BaseController;
import xin.yogee.ms.module.sys.entity.SysUser;
import xin.yogee.ms.module.sys.service.ISysUserService;

import cn.hutool.core.map.MapUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前台用户 前端控制器
 * </p>
 *
 * @author jackieQ
 * @since 2019-02-14
 */
@RestController
@RequestMapping("/user/sysUser")
public class SysUserController extends BaseController {

    private static String PASSWORD = "yunlu.2019";

    @Autowired
    private ISysUserService iSysUserService;

    @RequestMapping(value = "/login")
    public String login(@RequestBody JSONObject json){
        Map<String, Object> dataMap = MapUtil.newHashMap();
        String userName = json.getString("userName");
        String password = json.getString("password");
        //超级管理员登录
        if ("admin".equals(userName)){
            if (!PASSWORD.equals(password)){
                return successJson("error",dataMap,"密码错误");
            }else {
                dataMap.put("id","0");
                dataMap.put("name","超级管理员");
                return successJson("success",dataMap,"获取成功");
            }
        }else {
            SysUser user = iSysUserService.getByPhone(userName);
            if (user != null){
                if (!SecureUtil.md5(password).equals(user.getPassword())){

                    return successJson("error",dataMap,"密码错误");
                }
            }else {
                return successJson("error",dataMap,"用户不存在，请检查用户账号");
            }
            return successJson("success",user,"获取成功");
        }
    }

    @RequestMapping(value = "/list")
    public String list(SysUser sysUser, HttpServletRequest request){
        String current = request.getParameter("currentPage") == null ? "1" : request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize");
        IPage<SysUser> personnelList = iSysUserService.getList(new Page<SysUser>(Long.valueOf(current),Long.valueOf(pageSize)));
        return successJson("success",personnelList,"获取成功");
    }

    @RequestMapping(value = "/store")
    public String store(SysUser sysUser, @RequestBody JSONObject json){

        String id = json.getString("id");
        String name = json.getString("name");
        String password = json.getString("password");
        String avatar = json.getString("avatar");
        String postId = json.getString("postId");
        String bankCard = json.getString("bankCard");
        String entryTime = json.getString("entryTime");
        String departId = json.getString("departId");
        String phone = json.getString("phone");
        String baseGoals = json.getString("baseGoals");
        String skillGoals = json.getString("skillGoals");
        String educationGoals = json.getString("educationGoals");

        sysUser.setId(id);
        sysUser.setName(name);
        sysUser.setPassword(SecureUtil.md5(password));
        sysUser.setAvatar(avatar);
        sysUser.setPostId(postId);
        sysUser.setEntryAt(entryTime);
        sysUser.setDepartId(departId);
        sysUser.setPhone(phone);

        boolean result = iSysUserService.saveOrUpdate(sysUser);
        if (!result){
            return successJson("error",new HashMap(),"操作失败");
        }
        return successJson("success",new HashMap(),"操作成功");
    }

    @RequestMapping(value = "/get")
    public String get(SysUser sysUser, @RequestBody JSONObject json){
        String id = json.getString("id");
        SysUser user = iSysUserService.getById(id);
        return successJson("success",user,"获取成功");
    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody JSONObject json){
        String id = json.getString("userId");
        iSysUserService.removeById(id);
        return successJson("success","","删除成功");
    }

    @RequestMapping(value = "/getByDepartId")
    public String getByDepartId(SysUser sysUser, @RequestBody JSONObject json){
        String departId = json.getString("departId");
        List<SysUser> userList = new ArrayList<SysUser>();
            if ("".equals(departId)){
            userList = iSysUserService.list();
        }else {
            userList = iSysUserService.getListByDepartId(departId);
        }
        return successJson("success",userList,"获取成功");
    }

    @RequestMapping(value = "/treeList")
    public String treeList(){
        List<SysUser> userList = iSysUserService.getTreeList();
        return treeList(userList);
    }


    @RequestMapping(value = "/all")
    public String list(@RequestBody JSONObject json){
        List<SysUser> userList = iSysUserService.list();
        return successJson("success",userList,"获取成功");
    }

}
